﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using WebApp.BaseDataSvc;

namespace WebApp
{
    public partial class Left : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.IsPermitFrm = false;
            WebCommon.CheckLogin(this, 0);
            this.DivMenuList.InnerHtml = GetHtml();
        }

        DataTable BindDataSource()
        {
            DataSet objDs = new DataSet();
            //xml文件放在App_Data目录下
            objDs.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/Menus.xml"));
            return objDs.Tables[0];
        }

        private string GetHtml()
        {
            StringBuilder sb = new StringBuilder();
            IEnumerable<TB_SysModule> datas = this.BaseDataProxy.GetUserMenus2((Session["User"] as TB_User).role, null);
            if (datas != null)
            {
                datas.OrderBy(p => p.menuOrder);
                var menuBars = from p in datas group p by p.pMenuName into g select g;
                int count = 0;
                foreach (var s in menuBars)
                {

                    //if (sysModule.ParentId == 0)
                    //{
                    sb.AppendLine("<table cellspacing='0' cellpadding='0' width='159' align='center' border='0'>");
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td width='23'>");
                    sb.AppendLine("<img height='25' src='images/Menu/box_topleft.gif' width='23'></td>");
                    sb.AppendLine("<td class='ttl' onclick='JavaScript:showHide(\"" + s.Key + "\");' width='129' background='images/Menu/box_topbg.gif'>");
                    sb.AppendLine(s.Key);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td width='7'>");
                    sb.AppendLine("<img height='25' src='images/Menu/box_topright.gif' width='7'></td>");
                    sb.AppendLine("</tr>");
                    sb.AppendLine("</table>");
                    sb.AppendLine("<table id='" + s.Key + "' style='display: none' cellspacing='0' cellpadding='0' width='159' align='center' border='0'>");
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td background='images/Menu/box_bg.gif' height='0px' width='159' colspan='3'>");
                    sb.AppendLine("<table width='157' border='0' cellpadding='2' cellspacing='1'>");
                    sb.AppendLine("<tbody>");

                    var menus = datas.Where(p => p.pMenuName == s.Key);
                    if (menus.Any())
                    {
                        foreach (TB_SysModule menu in menus)
                        {
                            //子菜单
                            sb.AppendLine("<tr>");
                            sb.AppendLine("<td class='table_none' onclick='javascript:NowShow(\"" + menu.moduleKey + "\",\"" + menu.url + "\");' onmousemove='javascript:TDOverORIn(\"" + menu.moduleKey + "\");' onmouseout='javascript:TDOverOROut(\"" + menu.moduleKey + "\");' id='" + menu.moduleKey + "'>");
                            sb.AppendLine("<img height='7' hspace='5' src='images/Menu/arrow.gif' width='5' align='bottom'>");
                            //sb.AppendLine("<a href =\"" + sysModuleC.PageUrl + "\">" + sysModuleC.MName + "</a>");
                            sb.AppendLine(menu.moduleName);
                            sb.AppendLine("</td>");
                            sb.AppendLine("</tr>");
                            //子菜单
                        }
                    }
                    sb.AppendLine("</tbody>");
                    sb.AppendLine("</table>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");
                    sb.AppendLine("</table>");
                    sb.AppendLine("<table cellspacing='0' cellpadding='0' width='159' align='center' border='0'>");
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td colspan='3'>");
                    sb.AppendLine("<img height='10' src='images/Menu/box_bottom.gif' width='159'></td>");
                    sb.AppendLine("</tr>");
                    sb.AppendLine("</table>");
                    // }
                }
            }
            return sb.ToString();
        }
    }
}
